<?php

namespace app\admin\controller;

use app\common\controller\Admin;
use app\common\serve\Buy;

/**
 * 审核
 * Class Audit
 * @package app\admin\controller
 */
class Audit extends Admin
{

    /**
     * 提现审核
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function withdraw()
    {

        $params = $this->request->param();
        $where = [];
        if (isset($params['uid']) && $params['uid'] > 0) {
            $where['uw.uid'] = $params['uid'];
        }

        if (isset($params['status']) && !empty($params['status'])) {
            $where['uw.withdraw_status'] = $params['status'];
        }

        $field = 'uw.id,uw.uid,uw.type,uw.withdraw_money,uw.withdraw_paid_money,uw.withdraw_fee,uw.withdraw_status,
        uw.create_time,uw.deal_id,uw.deal_user,ub.bank_name,ub.card_id';

        $db = model('UserWithdraw');

        $list = $db->alias('uw')
            ->field($field)
            ->join('__USER_BANK__ ub', 'ub.uid=uw.uid', 'left')
            ->where($where)
            ->order('withdraw_status ASC, uw.id DESC')
            ->paginate(15, false, $this->request->param());

        $this->assign('list', $list);
        $this->assign('page', $list->render());

        $this->setMeta('提现审核信息');
        $this->assign('status', $db->withdraw_status);
        $this->assign('param', $params);
        return $this->fetch();
    }

    /**
     * 提现审核
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function review()
    {
        if ($this->request->isPost()) {
            $params = $this->request->param();

            $params['admin_uid'] = session('user_auth.uid');
            $params['admin_username'] = session('user_auth.username');

            $result = \app\common\serve\User::withdrawAudit($params);
            if (is_array($result) && $result['code'] == 1) {
                $this->success($result['msg'], url('withdraw'));
            } else {
                $this->error($result['msg']);
            }
        }

        $id = $this->request->param('id');
        if ($id < 1) $this->error('提现记录参数不正确！');

        $field = 'uw.id,uw.uid,uw.type,uw.withdraw_money,uw.withdraw_paid_money,uw.withdraw_fee,uw.withdraw_status,
        uw.create_time,uw.deal_id,uw.deal_user,uw.deal_info,ub.full_name,ub.bank_name,ub.card_id,u.phone';

        $withdraw = model('UserWithdraw')->alias('uw')->field($field)
            ->join('__USER__ u', 'uw.uid=u.id', 'LEFT')
            ->join('__USER_BANK__ ub', 'ub.uid=uw.uid', 'LEFT')
            ->where(['uw.id' => $id])
            ->find();

        $this->assign('info', $withdraw);

        $this->setMeta('财务审核信息');
        return $this->fetch();
    }

    /**
     * 返佣列表
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function rebate()
    {
        if ($this->request->isPost()) {

            $ids = $this->request->param('ids', '');
            if (!$ids) $this->error('缺少参数');

            $result = Buy::rebate(explode(',', $ids));
            if (is_array($result) && $result['code'] == 1) {
                $this->success($result['msg'], url('rebate'));
            } else {
                $this->error($result['msg']);
            }
        }

        $list = model('UserRebate')
            ->where([])
            ->order('status DESC, id DESC')
            ->paginate(15, false, $this->request->param());

        $this->assign('list', $list);
        $this->assign('page', $list->render());

        $this->setMeta('返佣列表');
        return $this->fetch();
    }


    /**
     * 购买审核
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
//    public function invest()
//    {
//        if ($this->request->isPost()) {
//
//            $ids = $this->request->param('ids', '');
//            if (!$ids) $this->error('缺少参数');
//
//            $result = Buy::auditPay([
//                'ids' => $ids,
//                'uid' => session('user_auth.uid'),
//                'status' => 1,
//            ]);
//
//            if (is_array($result) && $result['code'] == 1) {
//                $this->success($result['msg'], url('invest'));
//            } else {
//                $this->error($result['msg']);
//            }
//        }
//
//        $list = model('BorrowInvest')
//            ->where([])
//            ->order('audit_status DESC, id DESC')
//            ->paginate(15, false, $this->request->param());
//
//        $this->assign('list', $list);
//        $this->assign('page', $list->render());
//
//        $this->setMeta('购买审核信息');
//        return $this->fetch();
//    }

    /**
     * 到期审核
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
//    public function sale()
//    {
//        if ($this->request->isPost()) {
//
//            $ids = $this->request->param('ids', '');
//            if (!$ids) $this->error('缺少参数');
//
//            $result = Buy::auditSale([
//                'ids' => $ids,
//                'uid' => session('user_auth.uid'),
//                'status' => 3,
//            ]);
//
//            if (is_array($result) && $result['code'] == 1) {
//                $this->success($result['msg'], url('invest'));
//            } else {
//                $this->error($result['msg']);
//            }
//        }
//
//        $list = model('BorrowInvest')
//            ->where([])
//            ->order('audit_status DESC, id DESC')
//            ->paginate(15, false, $this->request->param());
//
//        $this->assign('list', $list);
//        $this->assign('page', $list->render());
//
//        $this->setMeta('到期审核列表');
//        return $this->fetch();
//    }

//    public function recharge()
//    {
//        if ($this->request->isPost()) {
//        }
//        return $this->fetch();
//    }

}